<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitereview
 * @copyright  Copyright 2012-2013 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Controller.php 6590 2013-04-01 00:00:00Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitereview_Widget_ListtypesCategoriesController extends Engine_Content_Widget_Abstract {

  public function indexAction() {

    $listingtypeId = $this->_getParam('listingtype_id', -1);
    $this->view->viewDisplayHR = $this->_getParam('viewDisplayHR', 1);
    if ($this->view->viewDisplay) {
      $element = $this->getElement();
      $this->view->widgetTitle = $element->getTitle();
      $element->setTitle('');
    }

    $params = array();
    $params['visible'] = 1;
    $params['member_level_allow'] = 1;
    $listingTypes = Engine_Api::_()->getDbtable('listingtypes', 'sitereview')->getListingTypes($listingtypeId, $params);
    $sitereviewListtypeCategories = Zend_Registry::isRegistered('sitereviewListtypeCategories') ?  Zend_Registry::get('sitereviewListtypeCategories') : null;
    $this->view->listingTypesCount = $listingTypesCount = Count($listingTypes);

    $beforeNavigation = $this->_getParam('beforeNavigation', 0);    
    if (empty($listingTypesCount)||($listingTypesCount <= 1 && ($beforeNavigation)) || empty($sitereviewListtypeCategories)) {
      return $this->setNoRender();
    }

    Engine_Api::_()->sitereview()->setListingTypeInRegistry($listingtypeId);
    $this->view->listingTypesArray = $listingTypesArray = array();
    //GET LISTING CATEGORY TABLE
    $this->view->tableCategory = $tableCategory = Engine_Api::_()->getDbTable('categories', 'sitereview');

    foreach ($listingTypes as $listingType) {

      $categoriesArray = array();
      $categories = $tableCategory->getCategories(null, 0, $listingType->listingtype_id, 0, 1);
      foreach ($categories as $category) {
        $subcategoriesArray = array();
        $subcategories = $tableCategory->getSubCategories($category->category_id);
        foreach ($subcategories as $subcategory) {
          $subsubcatgories = $tableCategory->getSubCategories($subcategory->category_id);
          $ssb_cat_count = count($subsubcatgories);
          if (empty($ssb_cat_count)) {
            $subsubcatgories = array();
          }
          $subcategoriesArray[$subcategory->category_id] = array(
              'subcategory' => $subcategory,
              'subsubcatgories' => $subsubcatgories
          );
        }

        $categoriesArray[$category->category_id] = array(
            'category' => $category,
            'subcategories' => $subcategoriesArray,
        );
      }

      $listingTypesArray[$listingType->listingtype_id] = array(
          'list_type' => $listingType,
          'categories' => $categoriesArray
      );
    }
    $this->view->listingTypesArray = $listingTypesArray;

    $this->view->requestAllParams = $requestAllParams = Zend_Controller_Front::getInstance()->getRequest()->getParams();
  }

}